package com.example.covid19.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Goods;
import com.example.covid19.entity.OrderForm;
import com.example.covid19.entity.OrderInfo;
import com.example.covid19.entity.User;
import com.example.covid19.mapper.GoodsMapper;
import com.example.covid19.mapper.OrderInfoMapper;
import com.example.covid19.service.GoodsService;
import com.example.covid19.service.OrderFormService;
import com.example.covid19.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.covid19.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-09-12
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private final OrderFormService orderFormService;

    private final UserService userService;
    private final GoodsService goodsService;


    public OrderInfoServiceImpl(OrderFormService orderFormService, UserService userService, GoodsService goodsService) {

        this.orderFormService = orderFormService;
        this.userService = userService;
        this.goodsService = goodsService;

    }

    @Override
    public Result userinfo(HttpServletRequest request) {
        LocalDateTime dateTime1 = LocalDateTime.now();
        OrderForm orderForm = new OrderForm();
        String uuid = UUID.randomUUID().toString();
        String userid = request.getHeader("satoken");
        orderForm.setOrdernumber(uuid);
        stringRedisTemplate.opsForValue().set(userid, uuid);
        orderForm.setUsername(userService.getOne(new QueryWrapper<User>().eq(
                "userid", StpUtil.getLoginId()
        )).getUsername());
        orderForm.setOrderdate(dateTime1);
        System.out.println(StpUtil.getLoginId());
        orderForm.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
        boolean flag = orderFormService.save(orderForm);
//        boolean flag = true;
        if (flag) {
            return Result.success(200, "信息填写成功", uuid);
        } else {
            return Result.fail(400, "信息填写错误", null);
        }

    }

    @Override //这个是提交购买 包含次数
    public Result gett(JSONArray jsonArray) {
        User user = new User();
        int i = 1;
        user.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
        user.setFrequency(userService.getOne(new QueryWrapper<User>().eq(
                "userid", StpUtil.getLoginId()
        )).getFrequency() + i);
        System.out.println(user.getFrequency());
        System.out.println(user.getStudentid());
        boolean flag = userService.updateById(user);
        if (flag) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }
        List<OrderInfo> orderInfos = JSONUtil.toList(jsonArray, OrderInfo.class);
        System.out.println(orderInfos);
        this.saveBatch(orderInfos);
        retrievals(jsonArray);

        return Result.success(200, "ok", jsonArray);
    }

    public void retrievals(JSONArray jsonArray) {
        List<OrderInfo> orderInfoList = JSONUtil.toList(jsonArray, OrderInfo.class);
        List<Integer> sidList = new ArrayList<>();
        Map<Integer, Integer> stringStringMap = new HashMap<>();
        for (OrderInfo orderInfo : orderInfoList) {
            sidList.add(orderInfo.getSid());
            stringStringMap.put(orderInfo.getSid(), orderInfo.getQuantity());
        }
        List<Goods> goodsList = goodsService.list(new QueryWrapper<Goods>()
                .in("sid", sidList));
        for (Goods goods : goodsList) {
            goods.setQuantity(
                    goods.getQuantity()
                            -
                            stringStringMap.get(goods.getSid())

            );
        }
        goodsService.updateBatchById(goodsList);
    }

    @Override //充值
    public Result rec(String rec) {
        User user = new User();
        System.out.println(rec);
        user.setUserid(Integer.parseInt(StpUtil.getLoginId().toString()));
        user.setBalance(userService.getOne(new QueryWrapper<User>().eq(
                "userid", StpUtil.getLoginId()
        )).getBalance() - Integer.parseInt(rec));

        user.setMoney(userService.getOne(new QueryWrapper<User>().eq(
                "userid", StpUtil.getLoginId()
        )).getMoney() + Integer.parseInt(rec));
        boolean flag = userService.updateById(user);
        if (flag) {

            return Result.success(200, "成功", null);
        } else {
            return Result.fail(400, "失败", null);
        }
    }
}
